import $url from "@/common/url";

class file{
	//多图批量上传
	async multipleUpload(event,fileList){
		let lists = [].concat(event.file)
		let fileListLen = fileList.length
		lists.map((item) => {
			fileList.push({
				...item,
				status: 'uploading',
				message: '上传中'
			})
		})
		
		for (let i = 0; i < lists.length; i++) {
			const result = await this.uploadImg(lists[i].url)
			let item = fileList[fileListLen]
			if(result.errno==0){			//上传成功
				fileList.splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result.data.url,
					path: result.data.path,
				}))
			}else{							//上传失败
				fileList.splice(fileListLen, 1, Object.assign(item, {
					status: 'failed',
					message: result.msg,
					url: ''
				}))
			}
			
			fileListLen++
		}
		return fileList
	}
	
	//单图片上传
	uploadImg(imgUrl,data={}){
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: $url.dl('common/file/images'),
				// #ifdef MP-ALIPAY
				fileType:'image',
				// #endif
				filePath: imgUrl,
				name: 'file',
				formData: data,
				success: (res) => {
					resolve(JSON.parse(res.data))
				},
				fail:(e)=>{
					console.log(e)
				}
			});
		})
	}
	
}
export default new file();